package com.deram.ctin.serv.member.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.serv.utils.MybatisUtils;
import com.deram.ctin.serv.member.entity.MemberFeedback;
import com.deram.ctin.serv.member.filter.MemberFeedbackFilter;
import com.deram.ctin.serv.member.mapper.MemberFeedbackMapper;
import com.deram.ctin.serv.member.service.MemberFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberFeedbackServiceImpl implements MemberFeedbackService {

    @Autowired
    private MemberFeedbackMapper memberFeedbackMapper;

    @Override
    public MemberFeedback find(Long id) {
        ValidateUtils.isId(id, "反馈ID");
        MemberFeedback feedback = memberFeedbackMapper.selectById(id);
        BusinessUtils.notNull(feedback, "反馈信息不存在");
        return feedback;
    }

    @Override
    public MemberFeedback save(MemberFeedback memberFeedback) {
        memberFeedbackMapper.insert(memberFeedback);
        return memberFeedback;
    }

    @Override
    public List<MemberFeedback> list(MemberFeedbackFilter filter) {
        EntityWrapper<MemberFeedback> ew = createEntityWrapper(filter);
        return memberFeedbackMapper.selectList(ew);
    }

    @Override
    public PageResult<MemberFeedback> search(MemberFeedbackFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo, pageSize);
        EntityWrapper<MemberFeedback> ew = createEntityWrapper(filter);
        return MybatisUtils.createPageResult(memberFeedbackMapper.selectPage(pagination, ew), pagination);
    }

    @Override
    public void delete(Long id) {
        memberFeedbackMapper.deleteById(find(id));
    }

    private EntityWrapper<MemberFeedback> createEntityWrapper(MemberFeedbackFilter filter) {
        MemberFeedback entity = new MemberFeedback();
        entity.setId(filter.getId());
        entity.setType(filter.getType());
        EntityWrapper<MemberFeedback> wrapper = new EntityWrapper<>(entity);
        return wrapper;
    }

}